/*
 * @Description: 以antd表单的形式缓存值 不会触发页面更新 对数组、对象只进行浅拷贝
 * @Author: liyuhao
 * @Date: 2021-06-25 10:06:51
 * @LastEditors: liyuhao
 * @LastEditTime: 2021-06-25 10:41:45
 */
import { useRef } from 'react'
import { FormInstance } from 'antd/lib/form'
import { ShallowCopy } from '@/utils/util'

class CacheForm {
  private value?: any

  constructor (defaultValue?: any) {
    this.value = defaultValue
  } 

  getFieldsValue () {
    return ShallowCopy(this.value)
  }

  setFieldsValue (value: any) {
    this.value = ShallowCopy(value)
  }

  resetFields () {
    this.value = undefined
  }
}

const UseCacheForm = (defaultValue?: any) => {
  const cache = useRef<FormInstance>(new CacheForm(defaultValue) as FormInstance)

  return cache.current
}

export default UseCacheForm